package fr.mma.livraison.channel;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

import com.google.gson.Gson;

import fr.mma.livraison.channel.dao.ChannelDao;
import fr.mma.livraison.channel.dto.Message;
import fr.mma.livraison.channel.persistance.Channel;

public class MessageServlet extends MessageEnabledServlet {

	/** */
	private static final long serialVersionUID = -5069396293764944303L;
	
	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		final String json = req.getParameter("json");
		if (StringUtils.isNotBlank(json)) {
			final Gson gson = new Gson();
			final Message message = gson.fromJson(json, Message.class);
			final ChannelDao dao = new ChannelDao();
			Channel channel = dao.getOrCreate(message.getChannelId());
			sendMessage(channel, message);
		}	
	}

}
